package oop.data;

import java.sql.SQLException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import oop.db.dao.AnswerDAO;

public class BaseQuestion {

	private long id;
	private String content;
	private int level;
	private SimpleCategory category;
	private Map<Long, Answer> answerMap = null;
	private SimpleUser author;
	
	public BaseQuestion(int id, String content, int level, SimpleCategory category,
			SimpleUser author) {
		this.id = id;
		this.content = content;
		this.level = level;
		this.category = category;
		this.author = author;
	}

	public Map<Long, Answer> getAnswerById() throws SQLException {
		if (answerMap == null) {
			answerMap = new HashMap<Long, Answer>();
			for (Answer answer : AnswerDAO.fetchByBaseQuestion(this)) {
				answerMap.put(answer.getId(), answer);
			}
		}
		return answerMap;
	}
	
	public Collection<Answer> getAnswers() throws SQLException {
		return getAnswerById().values();
	}
	
	public void addAnswer(Answer a) {
		//XXX implements 
	}
	
	public void removeAnswer(Answer a) {
		//XXX implements 
	}

	public long getId() {
		return id;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public SimpleCategory getCategory() {
		return category;
	}

	public void setCategory(SimpleCategory category) {
		this.category = category;
	}

	public void setAuthor(SimpleUser author) {
		this.author = author;
	}

	public SimpleUser getAuthor() {
		return author;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof BaseQuestion) {
			BaseQuestion question = (BaseQuestion) obj;
			return id == question.id;
		}
		return super.equals(obj);
	}
	
	@Override
	public int hashCode() {
		return new Long(id).hashCode();
	}
	
}
